﻿using System;
using GraphProcessor;
using IQIGame.Onigao.Logic;

namespace IQIGame.Onigao.GamePlay.Editors
{
    [System.Serializable, NodeMenuItem(SkillEditorConst.Battle + "/" + SkillEditorConst.Skill + "/" + SkillEditorConst.Condition, typeof(SkillEditorGraph))]
    public class SkillConditionNode : BaseNode
    {
        public override string name => "SkillCondition";

        public int formula;

        [Input(name = "From")] public SkillEditorCondition from;

        [Output(name = "True")] public SkillEditorCondition truePart;

        [Output(name = "False")] public SkillEditorCondition falsePart;

        protected override void Process()
        {
            base.Process();

            var rCondition = new SkillConditionConfig()
            {
                formulaID = this.formula,
            };

            if (this.from != null)
            {
                var rData = this.from;
                switch (rData.type)
                {
                    case ESkillEditorConditionType.True:
                    case ESkillEditorConditionType.False:
                        throw new Exception("Condition 不支持嵌套");
                    case ESkillEditorConditionType.All:
                        if (rData.part != null)
                        {
                            rData.part.condition = rCondition;
                        }
                        else if (rData.trigger != null)
                        {
                            rData.trigger.condition = rCondition;
                        }

                        break;
                }
            }

            this.truePart = new SkillEditorCondition(rCondition, ESkillEditorConditionType.True);
            this.falsePart = new SkillEditorCondition(rCondition, ESkillEditorConditionType.False);
        }
    }

    public class SkillEditorCondition
    {
        public SkillConditionConfig condition;
        public ESkillEditorConditionType type;
        public SkillPartConfig part;
        public TriggerConfig trigger;

        public SkillEditorCondition(SkillConditionConfig cond, ESkillEditorConditionType type)
        {
            this.condition = cond;
            this.type = type;
        }

        public SkillEditorCondition()
        {
            this.type = ESkillEditorConditionType.All;
        }
    }

    public enum ESkillEditorConditionType
    {
        All,
        True,
        False
    }
}